package hk.ces.pos.logon;

import hk.ces.spring.service.CustomerManager;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

/**
 *
 * @author Silence
 */
public class LogonController implements Controller
{
  
  private String pageView;
  Logger logger = Logger.getLogger("LogonController");
  private CustomerManager customer;
  
  public void setPageView(String pageView)
  {
    this.pageView = pageView;
  }
  
  public void setCustomer(CustomerManager customer)
  {
    this.customer = customer;
  }
  
  @Override
  public ModelAndView handleRequest(HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception
  {
    String username = hsr.getParameter("username");
    String password = hsr.getParameter("password");
    logger.info(username + ":" + password);
    if (username == null || username.equals(""))
    {
      username = "silence";
    }
    if (password == null || password.equals(""))
    {
      password = "silence";
    }
    Map result = customer.findByUsernameandPassword(username, password);
    if (!result.isEmpty())
    {
      return new ModelAndView(pageView, "username", username);
    }
    else
    {
      return new ModelAndView("test.jsp", "errorMessage", "username or password error!");
    }
  }
}
